/* Copyright (c) 2009 yinquan@foxmount.com
   See the file LICENSE.txt for licensing information. */

/*
    模拟鼠标点击元素，传入所在的document和要点击的元素
*/
function emulateClick(doc, ele) {
  //jsdump("emulateClick: " + ele.id);
  var evt = doc.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, doc.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var result = ele.dispatchEvent(evt);
  //jsdump("click result: " + result);
}

/* 页面加载完成后添加事件 */
function lijiPageLoad() {
  fillLijiLogin();
}

function lijiPageUnload() {
  $("liji-browser").removeEventListener("load", lijiPageLoad, false);
}

function shabaPageLoad() {
  fillShabaLogin();
}

function shabaPageUnload() {
  $("shaba-browser").removeEventListener("load", shabaPageLoad, false);
}

function newballPageLoad() {
  fillNewballLogin();
}

function newballPageUnload() {
  $("newball-browser").removeEventListener("load", newballPageLoad, false);
}

/*
    自动填充登陆信息
*/

function fillMacauLogin() {

}

function fillLijiLogin() {
  var maindoc = $("liji-browser").contentDocument;
  try {
    var doc = maindoc.getElementById("topFrame").contentDocument;
    var inputs = doc.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].id == 'id' && inputs[i].name == 'id') {
        var username = getStringPreference("liji.username", false);
        inputs[i].value = username;
        inputs[i].value
      }
      if (inputs[i].id == 'password' && inputs[i].name == 'password') {
        var password = getStringPreference("liji.password", false);
        inputs[i].value = password;
        inputs[i].value
      }
    }
    //lijiPageUnload();

  } catch(ex) {
    //jsdump("fillLijiLogin: " + ex);
  }

}

function fillShabaLogin() {
  var maindoc = $("shaba-browser").contentDocument;
  try {
    var doc = maindoc.getElementById("topFrame").contentDocument;
    var inputs = doc.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].id == 'txtID' && inputs[i].name == 'txtID') {
        var username = getStringPreference("shaba.username", false);
        inputs[i].value = username;

      }
      if (inputs[i].id == 'txtPW' && inputs[i].name == 'txtPW') {
        var password = getStringPreference("shaba.password", false);
        inputs[i].value = password;

      }
    }
    //shabaPageUnload();
  } catch(ex) {
    jsdump("fillShabaLogin: " + ex);
  }
}

function fillNewballLogin() {
  var doc = $("newball-browser").contentDocument;
  try {
    var inputs = doc.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].id == 's21UserIdTextBox') {
        inputs[i].value = getStringPreference("newball.username", false);

      } else if (inputs[i].id == 's21PassWordTextBox') {
        inputs[i].value = getStringPreference("newball.password", false);
      }
    }
  } catch(ex) {
    jsdump(ex);
  }
}

/*
    进行自动投注处理
*/

function autoBetValue(name, betValue) {
  if (name == sites.macau) {
    autoMacauBet(betValue);

  } else if (name == sites.liji) {
    autoLijiBet(betValue);

  } else if (name == sites.shaba) {
    autoShabaBet(betValue);

  } else if (name == sites.newball) {
    autoNewballBet(betValue);

  } else if (name == sites[4]) {

}
}
/*
    澳门网：
    Frame:mainFrame
    subFrame:<frame scrolling="auto" noresize="" name="betslipFrame"
        src="/cust?action=go_betslip&amp;adv_betslip="/>
    输入：
    <input type="text" onkeypress="return validate_stake(this,event)"
        onkeyup="stake_changed(this)" value="" maxlength="12" size="6"
        id="stake_4566903" name="stake_4566903"/>
    投注：
    <input type="button" onclick="submit_bets()" value="投注"/>
    清除：
    <input type="button" onclick="clear_selns()" value="清除"/>
*/
function autoMacauBet(betVaule) {
  var maindoc = $("macau-browser").contentDocument;
  try {
    var mainFrameDoc = maindoc.getElementsByName("mainFrame")[0].contentDocument;
    var doc = mainFrameDoc.getElementsByName('betslipFrame')[0].contentDocument;
    var inputs = doc.getElementsByTagName("input");
    var betButton = null;
    for (var i = 0; i < inputs.length; i++) {
      if ((inputs[i].id.search('stake') > -1) && (inputs[i].name.search('stake') > -1)) {
        inputs[i].value = betVaule;
        continue;
      }
      var type = inputs[i].getAttribute('type');
      var onclick = inputs[i].getAttribute('onclick');
      if (type != undefined && onclick != undefined && type == 'button' && onclick == "submit_bets()") {
        betButton = inputs[i];
        continue;
      }
    }
    if (betButton) {
      emulateClick(doc, betButton);
    }
    //lijiPageUnload();

  } catch(ex) {
    jsdump("autoMacauBet: " + ex);
  }
}

/*
    利记网：
    frame:	leftFrame
    输入框： 
    <input type="text" onkeypress="return stakeFormat(this,event);"
    onkeyup="onKeyUpStake(this)" onkeydown="if(event.keyCode==13) placebet(0,0);"
    value="" class="TicketInput" name="stake" id="stake"/>
    投注：
    <td onclick="return placebet();" onmouseover="className='TextOver'"
    onmouseout="className='Text'" class="Text">進行投注</td>
    取消：
    <td onmouseover="className='TextOver'" onmouseout="className='Text'"
    onclick="openBetlistWaiting();" class="Text">取消</td> 
*/
function autoLijiBet(betValue) {
  var maindoc = $("liji-browser").contentDocument;
  try {
    var doc = maindoc.getElementById("leftFrame").contentDocument
    var inputs = doc.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; i++) {
      if ((inputs[i].id == 'stake') && (inputs[i].name == 'stake')) {
        inputs[i].value = betValue;
        break;
      }
    }
    var betButton = null;
    var textClicks = doc.getElementsByClassName('Text');
    for (var i = 0; i < textClicks.length; i++) {
      var onclick = textClicks[i].getAttribute('onclick');
      if (onclick != undefined && onclick == "return placebet();") {
        betButton = textClicks[i];
      }
    }
    if (betButton) {
      emulateClick(doc, betButton);
    }
    //lijiPageUnload();

  } catch(ex) {
    jsdump("autoLijiBet: " + ex);
  }

}

/*
    沙巴网：
    Frame:  leftFrame
    输入框：
    <input type="text" onkeyup="payOutOnKU(this, event)"
    onkeypress="return(validateOnKP(this, event,this.selectionStart));"
    onkeydown="validateOnKD(this, event)" style="ime-mode: disabled;" size="10"
    name="BPstake" id="BPstake"/>
    投注：
    <input type="submit" value="確認投注" class="font11" name="btnBPSubmit" id="btnBPSubmit"/>
    取消：
    <input type="button" onclick="javascript:ReloadWaitingBetList('yes','no','1');"
    value="取消" class="font11"/>
 
*/
function autoShabaBet(betValue) {
  var maindoc = $("shaba-browser").contentDocument;
  jsdump("autoShabaBet: started");
  try {
    var doc = maindoc.getElementById("leftFrame").contentDocument
    var inputs = doc.getElementsByTagName("input");

    var betButton = null;
    for (var i = 0; i < inputs.length; i++) {
      if ((inputs[i].id == 'BPstake') && (inputs[i].name == 'BPstake')) {
        inputs[i].value = betValue;
        continue;
      }
      if ((inputs[i].id == 'btnBPSubmit') && (inputs[i].name == 'btnBPSubmit')) {
        betButton = inputs[i];
        continue;
      }
    }
    jsdump("autoShabaBet: betButton: " + betButton);
    if (betButton) {
      emulateClick(doc, betButton);
    }

  } catch(ex) {
    jsdump("autoShabaBet: " + ex);
  }
  jsdump("autoShabaBet: ended");
}

/*
    <input type="text" onkeyup="objBS.OnStakeChange();" maxlength="12" size="13"
    class="stakeEntry" id="txtStake" oncut="return false" onpaste="return false"/>
    
    <div onclick="objBS.PlaceBet();" style="cursor: pointer; width: 100px; float: left;">
*/
function autoNewballBet(betValue) {
  var maindoc = $("newball-browser").contentDocument;
  jsdump("autoNewballBet: started");
  try {
    var doc = maindoc.getElementById("fraLeftMenu").contentDocument
    var inputs = doc.getElementsByTagName("input");

    var betButton = null;
    for (var i = 0; i < inputs.length; i++) {
      if ((inputs[i].id == 'txtStake') && (inputs[i].className == 'stakeEntry')) {
        inputs[i].value = betValue;
        break;
      }
    }
    var div = doc.getElementById('divBetSlip');
    var divs = div.getElementsByTagName('div');
    for (var i = 0; i < divs.length; i++) {
      if (divs[i].getAttribute('onclick') == 'objBS.PlaceBet();') {
        betButton = divs[i];
        break;
      }
    }
    jsdump("autoNewballBet: betButton: " + betButton);
    if (betButton) {
      emulateClick(doc, betButton);
    }

  } catch(ex) {
    jsdump("autoNewballBet: " + ex);
  }
  jsdump("autoNewballBet: ended");
}

/*
    自动点击要投注的水位
*/
function clickBetLevel(name, levelID) {
  if (name == sites.liji) {
    clickLijiLevel(levelID);

  } else if (name == sites.macau) {
    clickMacauLevel(levelID);

  } else if (name == sites.newball) {
    clickShabaLevel(levelID);

  } else if (name == sites.shaba) {
    clickShabaLevel(levelID);

  } 
}
/*
    澳门网：
    <a href="javascript:add_bir_bet('4566828','1100','1000','0.0','Y','3','12','2|0')"
        class="odds">2.1</a>
*/

function clickMacauLevel(levelID) {
  var maindoc = $("macau-browser").contentDocument;
  try {
    var frameName = getStringPreference("macau.data.frame", false);
    var mainFrame = maindoc.getElementsByName("mainFrame")[0].contentDocument;
    var frame = mainFrame.getElementsByName(frameName);
    var doc = frame[0].contentDocument;
    var tableId = getStringPreference("macau.data.tableid", false);
    var table = doc.getElementById(tableId);
    var tbody = table.getElementsByTagName("tbody")[0];
    var links = tbody.getElementsByTagName("a");    
    for (var i = 0; i < links.length; i++) {
      if (links[i].href.search(levelID) > -1) {
        emulateClick(doc, links[i]);
        break;
      }
    }

  } catch(ex) {
    jsdump(ex);
  }
}

/*
    利记网：
    
*/
function clickLijiLevel(levelID) {
  var maindoc = $("liji-browser").contentDocument;
  try {
    var doc = maindoc.getElementById("mainFrame").contentDocument;
    var divTable = doc.getElementById("events");
    var contentTable = divTable.getElementsByClassName("ContentTable")[0];
    var spans = contentTable.getElementsByTagName('span');
    var id = findLijiLevelID(levelID);    
    for (var i = 0; i < spans.length; i++) {
      var onclick = spans[i].getAttribute('onclick');
      var newID = findLijiLevelID(onclick);      
      if (newID == id) {
        emulateClick(doc, spans[i]);
        break;
      }
    }
  } catch(ex) {
    jsdump(ex);
  }
}

/*
    沙巴网
    
*/
function clickShabaLevel(levelID) {
  var maindoc = $("shaba-browser").contentDocument;
  try {
    var doc = maindoc.getElementById("mainFrame").contentDocument;
    var divTable = doc.getElementById("oTableContainer_1");
    var ta = divTable.firstChild;
    var table = divTable.childNodes[0];
    var tbody = table.getElementsByTagName("tbody")[0];
    var links = tbody.getElementsByTagName('a');
    var id = findShabaLevelID(levelID);
    for (var i = 0; i < links.length; i++) {
      if (links[i].href.search(id) > -1) {
        emulateClick(doc, links[i]);
        break;
      }
    }
  } catch(ex) {
    jsdump(ex);
  }
}

/*
    新球网
    
*/
function clickNewballLevel(levelID) {
  var maindoc = $("newball-browser").contentDocument;
  try {
    var doc = maindoc.getElementById("fraOdds").contentDocument;
    var div = doc.getElementById("divOddsTableRB");
    var tables = div.getElementsByTagName("table");
    var footTable = null;
    for (var i = 0; i < tables.length; i++) {
      if (tables[i].className == "ot") {
        footTable = tables[i];
        break;
      }
    }
    if (footTable) {
      var tbody = footTable.getElementsByTagName("tbody")[0];
      var link = tbody.getElementById(levelID);
      if (link) {
        emulateClick(doc, link);
      }
    }

  } catch(ex) {
    jsdump(ex);
  }
}